//
//  CCQMainViewController.swift
//  NEWweibo
//
//  Created by 神威 on 16/9/19.
//  Copyright © 2016年 ccq. All rights reserved.
//

import UIKit
import SVProgressHUD
class CCQMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let ccqTabBar = CCQTabBar()
        //  设置代理对象
        ccqTabBar.ccqDelegate = self
        

    
        ccqTabBar.composeButtonClosure = { [weak self] in
            
            print("我是闭包调用过来的")
            
            
            if !CCQUserAccountViewModel.sharedUserAccountViewModel.isLogin {
                SVProgressHUD.showError(withStatus: "请先登录, 亲~")
                return
            }
            
            //  代码执行到此表示已经登录
            let composeView = CCQComposeView()
            //  最上层的window
            let window = UIApplication.shared.windows.last!
            window.addSubview(composeView)
            
        }
        

        
        setValue(ccqTabBar, forKey: "tabBar")
        
        
        //  添加子视图控制器
        addChildViewController(childController: CCQHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: CCQMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: CCQDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: CCQProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
        
        
    }
    
    // 添加子视图控制器的方法重载
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        
        //  设置tabbarItem的图片和文字
        childController.tabBarItem.image = UIImage(named: imageName)
        //  修改图片的渲染方式
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.withRenderingMode(.alwaysOriginal)
        //  设置文字选中的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  设置文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14)], for: .normal)
      
        //  合并成一句代码显示标题
        childController.title = title
        
        //  创建导航视图控制器
        let nav = CCQNavigationViewController(rootViewController: childController)
        
        
        //  添加子视图控制器
        addChildViewController(nav)
        
        
    }
    
    
    
    
    
}
//  实现代理方法
extension CCQMainViewController: CCQTabBarDelegate {
    
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
    
}
